package com.efast.cafe.portal.service.api.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.efast.cafe.portal.bean.api.ApiObjDefQueryBean;
import com.efast.cafe.portal.dao.api.ApiObjDefDao;
import com.efast.cafe.portal.entity.api.ApiObjDef;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.cafe.portal.service.api.IApiObjDefService;
import com.github.pagehelper.PageHelper;

@Service
@Transactional
public class ApiObjDefServiceImpl implements IApiObjDefService {

	@Autowired
	private ApiObjDefDao apiObjDefDao;

	/**
	 * 查询uri 对象列表
	 */
	@Override
	public ApiObjDefQueryBean queryPageList(ApiObjDefQueryBean queryBean) {
		PageHelper.startPage(queryBean.getStart(), queryBean.getLength());
		queryBean.setResultList(apiObjDefDao.queryPageList(queryBean));
		queryBean.setTotalRows(apiObjDefDao.queryPageCount(queryBean));
		return queryBean;
	}

	/**
	 * 新增对象
	 */
	@Override
	public Integer addApiObj(ApiObjDef apiObjDef) {
		apiObjDef.setIsDel((byte) 0);
		apiObjDef.setOptCounter(1);
		return apiObjDefDao.addApiObj(apiObjDef);
	}

	/**
	 * 编辑对象
	 */
	@Override
	public void editApiObj(ApiObjDef apiObjDef) {
		 apiObjDefDao.editApiObj(apiObjDef);
	}

	/**
	 * 通过ID删除对象
	 */
	@Override
	public void delApiObj(Integer id) {
		 apiObjDefDao.delApiObj(id);
	}

	/**
	 * 通过ID查询对象详细信息
	 */
	@Override
	public ApiObjDef getApiObjDetail(Integer id) {
		return apiObjDefDao.getApiObjDetail(id);
	}

	/**
	 * 获取相关uri关联对象 列表以及未关联对象列表
	 * uriSelected   uri关联对象列表
	 * uriUnSelected  未关联对象列表
	 */
	@Override
	public Map<String, List<ApiObjDef>> multiple(ApiObjDefQueryBean queryBean) {
		Map<String, List<ApiObjDef>> map=new HashMap<>();
		List<ApiObjDef> uriSelected=new ArrayList<>();
		if(queryBean.getUriId()!=null){
		 uriSelected=apiObjDefDao.queryListByUri(queryBean);
		}
		map.put("uriSelected", uriSelected);
		queryBean.setListObj(uriSelected);
		List<ApiObjDef> uriUnSelected=apiObjDefDao.queryUnListByUri(queryBean);
		map.put("uriUnSelected", uriUnSelected);
		return map;
	}

}
